package ar.edu.unsam.algo3.plantasvszombies.ui.windows

import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.AlmanaqueAppModel
import org.uqbar.arena.layout.ColumnLayout
import org.uqbar.arena.widgets.Button
import org.uqbar.arena.widgets.Panel
import org.uqbar.arena.widgets.TextBox
import org.uqbar.arena.widgets.tables.Column
import org.uqbar.arena.widgets.tables.Table
import org.uqbar.arena.windows.SimpleWindow
import org.uqbar.arena.windows.WindowOwner
import ar.edu.unsam.algo3.plantasvszombies.dominio.Zombie

//Ventana
class AlmanaqueWindow extends SimpleWindow<AlmanaqueAppModel>{
		new(WindowOwner parent) {
		super(parent, new AlmanaqueAppModel)
		modelObject.buscar() //Corre este método cuando se crea la ventana
	}
	
	override def createMainTemplate(Panel mainPanel) {
		title = "Almanaque de zombies"
		taskDescription = "Ingresa el nombre del zombie"

		super.createMainTemplate(mainPanel)
		this.createResultsGrid(mainPanel)
	}
	
	override protected addActions(Panel actionsPanel) {
		new Button(actionsPanel)
			.setCaption("Buscar")
			.onClick [ | modelObject.buscar ] 
			.setAsDefault
			.disableOnError
	}
	
	override def createFormPanel(Panel mainPanel) {
		var searchFormPanel = new Panel(mainPanel)
		searchFormPanel.setLayout(new ColumnLayout(2))
		new TextBox(searchFormPanel).bindValueToProperty("nombre")
	}
	
	def protected createResultsGrid(Panel mainPanel) {
		var table = new Table<Zombie>(mainPanel, typeof(Zombie))
		table.heigth = 200
		table.width = 450
		table.bindItemsToProperty("result")
		table.bindValueToProperty("selectedItem")
		this.describeResultsGrid(table)
	}
	
	def void describeResultsGrid(Table<Zombie> table) {
		new Column<Zombie>(table) //
			.setTitle("Nombre")
			.setFixedSize(150)
			.bindContentsToProperty("nombre")

		new Column<Zombie>(table) //
			.setTitle("Ataque")
			.setFixedSize(50)
			.bindContentsToProperty("ataque")

		new Column<Zombie>(table)
			.setTitle("Defensa")
			.setFixedSize(50)
			.bindContentsToProperty("defensa")

		new Column<Zombie>(table)
			.setTitle("Descripcion")
			.setFixedSize(100)
			.bindContentsToProperty("descripcion")
	}
}